package xyz.riceball.usercore.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.riceball.framework.core.annotation.RbLog;
import xyz.riceball.framework.core.annotation.RbSysRequest;
import xyz.riceball.framework.core.bean.ApiController;
import xyz.riceball.framework.core.bean.ApiR;
import xyz.riceball.framework.core.bean.PageParam;
import xyz.riceball.usercore.entity.dto.UserDTO;
import xyz.riceball.usercore.entity.po.UserPO;
import xyz.riceball.usercore.service.UserService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author xiaovcloud
 * @since 2022-04-24 20:17:50
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/rb/api/usercore/user")
@RbSysRequest
public class UserController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param pageParam   分页对象
     * @param userPO 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "分页查询用户")
    @GetMapping
    @RbLog(description = "分页查询用户")
    public ApiR selectPage(PageParam pageParam, UserPO userPO) {
        return success(userService.selectPage(PageParam.of(pageParam),userPO));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过id查询用户")
    @GetMapping("{id}")
    @RbLog(description = "通过id查询用户")
    public ApiR selectOne(@PathVariable Serializable id) {
        return success(userService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param userPO 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增用户")
    @PostMapping
    @RbLog(description = "新增用户")
    public ApiR<Void> insert(@RequestBody UserPO userPO) {
        boolean save = userService.add(userPO);
        return ApiR.msg(save, "新增用户成功", "新增用户失败");
    }

    /**
     * 修改数据
     *
     * @param userPO 实体对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改用户")
    @PutMapping
    @RbLog(description = "修改用户")
    public ApiR<Void> update(@RequestBody UserPO userPO) {
        boolean edit = userService.edit(userPO);
        return ApiR.msg(edit, "编辑用户成功", "编辑用户失败");
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @ApiOperation(value = "删除用户")
    @RbLog(description = "删除用户")
    @DeleteMapping
    public ApiR<Void> delete(@RequestBody List<String> ids) {
        return userService.delete(ids);
    }

    /**
     * 获取当前用户
     *
     * @return
     */
    @ApiOperation(value = "获取当前用户")
    @RbLog(description = "获取当前用户")
    @GetMapping("current")
    public ApiR<UserDTO> getCurrentUser(@RequestHeader String token) {
        return success(userService.getCurrentUser(token));
    }
}

